﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Collections.ObjectModel;
using cecbData;
using System.Data;
using cecbClasses.Data_Adapters;
using cecbClasses.Data_Adapters.Project;

namespace CECB.Activity
{
    /// <summary>
    /// Interaction logic for Project_Activity_Window.xaml
    /// </summary>
    public partial class Project_Activity_Window : Window
    {
        public ObservableCollection<Activities> ModelListe { get; set; }
        Activities model;
        public Project_Activity_Window()
        {
            InitializeComponent();
            isprojectSelected = false;
           

            listBoxActivities.ItemsSource = new Func_Projects().getAllProjects();

            ModelListe = new ObservableCollection<Activities>();
            DataContext = this;

            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();

            ProjectAutocompleteTextBox.ItemsSource = slid.getProjectDetails();
            if (listBoxActivities.Items.Count > 0)
            {
                groupBox.Visibility = System.Windows.Visibility.Visible;
            }
            else groupBox.Visibility = System.Windows.Visibility.Hidden;

        }
        bool isprojectSelected = false;
        private void button1_Click(object sender, RoutedEventArgs e)
        {


            if (comboBoxComponents.SelectedIndex != -1)
            {
                if (textBoxAddActivity.Text.Length != 0)
                {
                    model = new Activities();
                    model.actv_phase = comboBoxComponents.Text;
                    model.actv_name = textBoxAddActivity.Text;
                    ModelListe.Add(model);
                    textBoxAddActivity.Clear();
                }
                else MessageBox.Show("Please Enter a activity", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else MessageBox.Show("Please Select a phase", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedIndex > -1)
                ModelListe.RemoveAt(dataGrid1.SelectedIndex);
            else
                MessageBox.Show("Nothing has selected to remove", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            //label3.Content = "";
            //label5.Content = "";

            //label6.Content = "";
            //labelProjectReference.Content = "";
            //listBoxActivities.ItemsSource = null;
            //listBoxActivities.Items.Clear();
            //ModelListe.Clear();
            //ProjectAutocompleteTextBox.Text = "";
            //textBoxActivitySearch.Text = "";
            //textBoxAddActivity.Text = "";
            //isprojectSelected = false;

        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            if (isprojectSelected)
            {
                if (dataGrid1.Items.Count != 0)
                {
                    if (comboBoxComponents.SelectedIndex != -1)
                    {

                        List<String> phase = new List<string>();
                        List<String> acti = new List<string>();

                        int rows = dataGrid1.Items.Count;
                        int rows1 = dataGrid1.Items.Count;
                        foreach (Activities row in dataGrid1.Items)
                        {

                            phase.Add(row.actv_phase);
                            acti.Add(row.actv_name);
                            rows--;
                            if (rows == 0)
                                break;
                        }


                        //if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().InsertActivity(phase, acti, rows1, labelProjectReference.Content.ToString(),false))
                        //{
                        //    MessageBox.Show(dataGrid1.Items.Count + " Activities inserted", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        //    textBoxAddActivity.Text = "";
                        //    ModelListe.Clear();

                        //}
                    }
                    else
                        MessageBox.Show("select the phace", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                }
                else MessageBox.Show("Add activities", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);



        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                 cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();
                 Projects act = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                 ProjectDetailsWrapPanel.DataContext = slid.getProjectDetailsByRef(act.proj_reference);
        
            }
            catch (Exception)
            {
                
                //throw;
            }
        }



        private void textBoxActivitySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxActivitySearch.Text.Length > 0)
            {
                 
                (FindResource("drop") as Storyboard).Begin(this);
                listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getActivityDetailsByName(textBoxActivitySearch.Text.Trim());
                if (listBoxActivities.Items.Count > 0)
                {
                    groupBox.Visibility = System.Windows.Visibility.Visible;
                }
                else groupBox.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void textBoxPhase_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBoxAddActivity_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxAddActivity.Text.Length > 0)
            {
                 
                (FindResource("drop") as Storyboard).Begin(this);
                listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getActivityDetailsByName(textBoxAddActivity.Text.Trim());

            }
        }

        private void ProjectAutocomplete_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                           cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();
            isprojectSelected = true;
            Projects act = (Projects)ProjectAutocompleteTextBox.SelectedItem;
            ModelListe.Clear();
            ProjectDetailsWrapPanel.DataContext = slid.getProjectDetailsByRef(act.proj_reference);
            }
            catch (Exception)
            {
               
            }
        }

        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<Activities> list = new List<Activities>();

            if (listBoxActivities.SelectedIndex != -1)
            {

                Activities name = (Activities)listBoxActivities.SelectedItem;

                String x = name.actv_name;
                String y = name.actv_phase;
                //   DssEntities cecbContext = new DssEntities();
                //     String y = cecbContext.Components.First(i => i.cmpt_reference == x).cmpt_name.ToString();
                //  MessageBox.Show(name.impt_name + "==="+ y);


                model = new Activities();

                //  model.impt_name= comboBoxComponents.Text;
                try
                {
                    model.actv_name = name.actv_name;
                    model.actv_phase = y;
                    ModelListe.Add(model);
                }
                catch (Exception)
                {

                }
            }
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ModelListe.RemoveAt(1);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void textBoxAddActivity_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }
    }
}
